package com.woniu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.RbacManager;
import com.woniu.entity.ResponseEntity;
import com.woniu.mapper.RbacManagerMapper;
import com.woniu.service.RbacManagerService;
import com.woniu.utils.SecurityCryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author Rain
 * @Description :
 * @date 2022/09/01 14:37
 */
@Service
@Transactional
public class RbacManagerServiceImpl implements RbacManagerService {

    @Autowired
    RbacManagerMapper mapper;

    @Override
    public ResponseEntity findAll(int pageNum, int pageSize, RbacManager rbac) {
        PageHelper.startPage(pageNum,pageSize);
        List<RbacManager> all = mapper.findAll(rbac);
        PageInfo<RbacManager> pageInfo = new PageInfo<>(all);
        return new ResponseEntity(200, "查询成功", pageInfo);
    }

    @Override
    public ResponseEntity deleteOne(int id) {
        int i = mapper.deleteOne(id);
        if (i > 0)
            return new ResponseEntity(200, "成功删除[" + id + "]这条数据", null);

        return new ResponseEntity(200, "失败", null);

    }

    @Override
    public ResponseEntity addOne(RbacManager rbac) {
        int count = mapper.findCountByName(rbac.getAccount());
        if (count > 0) {
            return new ResponseEntity(500, "新增失败,账户名重复", null);
        } else {
            String newP = SecurityCryUtil.generateEncryptedPassword(rbac.getPassword(), rbac.getAccount());
            rbac.setPassword(newP);
            int i = mapper.addOne(rbac);
            if (i > 0)
                return new ResponseEntity(200, "新增成功", null);
            else
                return new ResponseEntity(500, "新增失败", null);
        }

    }

    @Override
    public ResponseEntity editOne(RbacManager rbac) {
        String newP = SecurityCryUtil.generateEncryptedPassword(rbac.getPassword(), rbac.getAccount());
        rbac.setPassword(newP);
        int count = mapper.findCountByNameOrId(rbac);
        if (count > 1) {
            return new ResponseEntity(500, "修改失败,账户名重复", null);
        } else {
            int i = mapper.editOne(rbac);
            if (i > 0)
                return new ResponseEntity(200, "修改[" + rbac.getAccount() + "]用户成功", null);
            else
                return new ResponseEntity(500, "修改失败", null);
        }
    }

    @Override
    public RbacManager findOneByName(String username) {

        return mapper.findOneByName(username);
    }

    @Override
    public ResponseEntity editOneStatus(RbacManager rbac) {
        int i = mapper.editOneStatus(rbac);
        if (i > 0)
            return new ResponseEntity(200, "修改id[" + rbac.getId() + "]状态成功", null);
        else
            return new ResponseEntity(500, "修改失败", null);
    }

    @Override
    public ResponseEntity initPwd(int id) {
        RbacManager rbacManager = mapper.findOneById(id);
        //初始化用户密码 123456  Hash算法都是非对称加密(不能解密)  提到外面去！
        String s = SecurityCryUtil.generateEncryptedPassword("123456", rbacManager.getAccount());
        int i = mapper.updateInitPwd(id,s);
        if (i>0){
            return new ResponseEntity(200,"初始化密码成功",null);
        }else {
            return new ResponseEntity(500,"初始化失败",null);
        }
    }
}
